package com.jyw.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jyw.App;
import com.jyw.R;
import com.jyw.network.Server;
import com.jyw.pojo.AllClass;
import com.jyw.utils.DimenUtils;
import com.squareup.picasso.Picasso;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author simonw
 * @version 1.0
 * @created 2016/12/31
 */

public class GaoDuanActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gaoduan);

        MagicIndicator magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator);
        final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

        final List<String> mTitleDataList = new ArrayList<>();
        mTitleDataList.add("高端服饰家纺");
        mTitleDataList.add("奢侈品皮具");
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(Color.GRAY);
                colorTransitionPagerTitleView.setSelectedColor(Color.BLACK);
                colorTransitionPagerTitleView.setText(mTitleDataList.get(index));
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        viewPager.setAdapter(new VPAdapter());
        ViewPagerHelper.bind(magicIndicator, viewPager);

        int tab_num = getIntent().getIntExtra("TAB_NUM", 0);
        if (tab_num == 1) {
            viewPager.setCurrentItem(1);
        }

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findViewById(R.id.btn_yuyue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GaoDuanActivity.this, OrderAddActivity.class);
                if (viewPager.getCurrentItem() == 0) {
                    intent.putExtra("class_data", "高端服饰家纺");
                } else {
                    intent.putExtra("class_data", "奢侈品皮具");
                }
                startActivity(intent);
            }
        });
    }

    private class VPAdapter extends PagerAdapter {
        View v0, v1;
        RecyclerView r0, r1;

        @SuppressLint("InflateParams")
        VPAdapter() {
            v0 = LayoutInflater.from(GaoDuanActivity.this).inflate(R.layout.page_gaoduan, null);
            v1 = LayoutInflater.from(GaoDuanActivity.this).inflate(R.layout.page_gaoduan, null);
            r0 = (RecyclerView) v0.findViewById(R.id.recycler);
            r1 = (RecyclerView) v1.findViewById(R.id.recycler);
            LinearLayoutManager mgr0 = new LinearLayoutManager(GaoDuanActivity.this);
            LinearLayoutManager mgr1 = new LinearLayoutManager(GaoDuanActivity.this);
            r0.setLayoutManager(mgr0);
            r1.setLayoutManager(mgr1);
            r0.addItemDecoration(new SpacesItemDecoration(DimenUtils.dip2px(GaoDuanActivity.this, 8f),
                    DimenUtils.dip2px(GaoDuanActivity.this, 4f)));
            r1.addItemDecoration(new SpacesItemDecoration(DimenUtils.dip2px(GaoDuanActivity.this, 8f),
                    DimenUtils.dip2px(GaoDuanActivity.this, 4f)));
            r0.setAdapter(new GaoDuanAdapter(0));
            r1.setAdapter(new GaoDuanAdapter(1));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (position == 0) {
                container.addView(v0);
                return v0;
            } else {
                container.addView(v1);
                return v1;
            }
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        class GaoDuanAdapter extends RecyclerView.Adapter<GaoDuanAdapter.GaoduanHolder> {
            private List<AllClass.Class> data;

            GaoDuanAdapter(int pos) {
                if (pos == 0) {
                    data = App.parent7s;
                } else {
                    data = App.parent8s;
                }
            }

            @SuppressLint("InflateParams")
            @Override
            public GaoduanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new GaoduanHolder(LayoutInflater.from(GaoDuanActivity.this).inflate(R.layout.item_gaoduan, null));
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onBindViewHolder(GaoduanHolder holder, int position) {
                Picasso.with(GaoDuanActivity.this).load(Server.SERVER + data.get(position).getImg()).into(holder.ivPic);
                holder.tvName.setText(data.get(position).getName());
                holder.tvPrice.setText(data.get(position).getPrice() + "   >>");
            }

            @Override
            public int getItemCount() {
                return data.size();
            }

            class GaoduanHolder extends RecyclerView.ViewHolder {
                ImageView ivPic;
                TextView tvName;
                TextView tvPrice;

                GaoduanHolder(View itemView) {
                    super(itemView);
                    ivPic = (ImageView) itemView.findViewById(R.id.iv_pic);
                    tvName = (TextView) itemView.findViewById(R.id.tv_name);
                    tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
                }
            }
        }
    }

    class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int leftRight;
        private int topBottom;

        SpacesItemDecoration(int leftRight, int topBottom) {
            this.leftRight = leftRight;
            this.topBottom = topBottom;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            //竖直方向的
            if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {
                //最后一项需要 bottom
                if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {
                    outRect.bottom = topBottom;
                }
                outRect.top = topBottom;
                outRect.left = leftRight;
                outRect.right = leftRight;
            } else {
                //最后一项需要right
                if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {
                    outRect.right = leftRight;
                }
                outRect.top = topBottom;
                outRect.left = leftRight;
                outRect.bottom = topBottom;
            }
        }
    }
}
